#----------------------------------------------------------------------------------------#
# This code snippet is used to delete the .svn directory that is created by subvertion.  #
# Creator: Jiawei                                                                        #
# Time: 2008/07/05                                                                       #
#----------------------------------------------------------------------------------------#


import os
import win32con
import win32api
import shutil

def deleteFiles(dir_name):
    """"""
    for svnRoot, svnDirs, svnFiles in os.walk(dir_name):
        for name in svnFiles:
            win32api.SetFileAttributes(os.path.join(svnRoot, name), win32con.FILE_ATTRIBUTE_NORMAL)
            try:
                os.remove(os.path.join(svnRoot, name))
            except (IOError, os.error):
                print "unable to delete the file " + os.path.join(svnRoot, name)
                logfile.write("unable to delete the file " + os.path.join(svnRoot, name))
            else:
                print "del " + os.path.join(svnRoot, name)
                logfile.write("del " + os.path.join(svnRoot, name) + "\n")
            

dirPathName = raw_input("Please input the directory name:")
fileArray = os.walk(dirPathName)
svnDirArray = []
logfile = open(dirPathName + "Log.txt", "a")
for root, dirs, files  in fileArray:
    for dir_name in dirs:
        if (dir_name == ".svn"):
            svnDirArray.append(os.path.join(root, dir_name))
if len(svnDirArray) != 0:
    for svnDirName in svnDirArray:
        try:
            deleteFiles(svnDirName)
            win32api.SetFileAttributes(svnDirName, win32con.FILE_ATTRIBUTE_NORMAL)
            shutil.rmtree(svnDirName)
        except (IOError, os.error):
            print "unable to delete " + svnDirName
            logfile.write("unable to delete " + svnDirName + "\n")
        else:
            print "Successfully deleted the directory " + svnDirName
            logfile.write("Successfully deleted the directory " + svnDirName + "\n")
else:
    print "There is no directory named .svn was found!"

	